package it.tommasocortonesi.scjp.cap4;

/*
 * Classe test per dichiarazione tipi primitivi.
 * Range e byte dei tipi primitivi:
 * 
 * byte			1 		[-2^7 , 2^7-1]
 * short		2 		[-2^15 , 2^15-1]
 * int			4		[-2^31 , 2^31-1]
 * long			8 		[-2^63 , 2^63-1]
 * float		4 		na
 * double		8 		na
 * char			2 		[0 , 2^16]
 */

public class PrimitivesDeclaration {
	
	public static void main(String[] args) {
		byte b1 = (byte)(maxValue(7) - 1);
		short s1 = (short)(maxValue(15) - 1);
		int i1 = (int)(maxValue(31) - 1);
		long l1 = (long)(maxValue(63) - 1);

		System.out.println("byte maxValue (2^7-1): " + b1);		// valori max
		System.out.println("short maxValue (2^15-1): " + s1);
		System.out.println("int maxValue (2^31-1): " + i1);
		System.out.println("long maxValue (2^63-1): " + l1);
		
		i1 = 128;												// effettuo un downcast
		b1 = (byte)i1;											// out of range
		System.out.println("int 128 => byte: " + b1);
		
		float f = 12.99f;										// arrotondamento da float
		b1 = (byte)f;											// a byte
		System.out.println("float 12.99 => byte: " + b1);
		
		char c = (char)s1;										
		System.out.println("char: " +  c);
		if (c <= i1) {											// il char si può castare 
			System.out.println("c <= i1");						// a int
		} else {
			System.out.println("c > i1");
		}
		
	}
	
	private static long maxValue(int l) {
		// metodo calcolo 2^l
		long val = 1L;
		for(int i = 0; i < l; i++) {
			val *= 2; 
		}
		return val;
	}

}
